package com.sdp.right.db.sys_flow_node_jump_version.service.impl;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.right.db.sys_flow_node_jump_version.mapper.SysFlowNodeJumpVersionMapper;
import com.sdp.right.db.sys_flow_node_jump_version.service.ISysFlowNodeJumpVersionService;
import com.sdp.right.entity.SysFlowNodeJumpVersion;
import com.sdp.right.entity.SysFlowTemplateVersion;
import com.sdp.right.service.RemoveSystemService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-07-17
 */
@Service
public class SysFlowNodeJumpVersionServiceImpl extends ServiceImpl<SysFlowNodeJumpVersionMapper, SysFlowNodeJumpVersion> implements ISysFlowNodeJumpVersionService,RemoveSystemService {

	@Override
	public void createFlowNodeJumpVersion(SysFlowTemplateVersion sysFlowTemplateVersion) {
		JSONObject jumpObj = JSONObject.parseObject(new String(Base64.getDecoder().decode(sysFlowTemplateVersion.getTemplateJumpData())));
		List<SysFlowNodeJumpVersion> list = new ArrayList<>();
		SysFlowNodeJumpVersion jump = null;
		JSONArray endNodeArr = null;
		JSONObject endObj = null;
		for (String key : jumpObj.keySet()) {
            JSONObject val = jumpObj.getJSONObject(key);
            endNodeArr = val.getJSONArray("endNodeArr");
            if(endNodeArr!=null && endNodeArr.size()>0) {
            	for(int i = 0;i<endNodeArr.size();i++) {
            		endObj = endNodeArr.getJSONObject(i);
            		jump = new SysFlowNodeJumpVersion();
                	jump.setSystemNo(sysFlowTemplateVersion.getSystemNo());
                    jump.setTemplateNo(sysFlowTemplateVersion.getTemplateNo());
                    jump.setTemplateVersion(sysFlowTemplateVersion.getTemplateVersion());
                    jump.setStartNodeLabel(val.getString("startNodeLabel"));
                    jump.setStartNodeNo(val.getString("startNodeId"));
                    jump.setEndNodeLabel(endObj.getString("endNodeLabel"));
                    jump.setEndNodeNo(endObj.getString("endNodeId"));
                    jump.setJumpCondition(endObj.getString("condition"));
                    jump.setIsReturn(endObj.getString("isReturn"));
                    list.add(jump);
            	}
            	
            }
        }
		saveBatch(list);
	}

	@Override
	public void clearHisFlowNodeJumpVersion(String version, String templateNo) {
		LambdaQueryWrapper<SysFlowNodeJumpVersion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.ne(SysFlowNodeJumpVersion::getTemplateVersion, version);
		queryWrapper.eq(SysFlowNodeJumpVersion::getTemplateNo, templateNo);
		remove(queryWrapper);
		
	}

	@Override
	public List<SysFlowNodeJumpVersion> querySysFlowNodeJumpVersion(String version, String templateNo, String nodeNo) {
		LambdaQueryWrapper<SysFlowNodeJumpVersion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowNodeJumpVersion::getTemplateVersion, version);
		queryWrapper.eq(SysFlowNodeJumpVersion::getTemplateNo, templateNo);
		queryWrapper.eq(SysFlowNodeJumpVersion::getStartNodeNo, nodeNo);
		return list(queryWrapper);
	}

	@Override
	public void removeFlowNodeJumpVersion(String templateNo) {
		LambdaQueryWrapper<SysFlowNodeJumpVersion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowNodeJumpVersion::getTemplateNo, templateNo);
		remove(queryWrapper);
		
	}

	@Override
	public void removeBySystemNo(String systemNo) {
		LambdaQueryWrapper<SysFlowNodeJumpVersion> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysFlowNodeJumpVersion::getSystemNo, systemNo);
		remove(queryWrapper);
		
	}

}
